﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class TablePropertyObject
    {
        public static TableProperty[] GetTableProperties()
        {
            return Query(null);
        }

        public static TableProperty GetTableProperty(int id)
        {
            TableProperty tableProperty = new TableProperty();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TablePropertyDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = TablePropertyDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable tableProperties = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            tableProperty.Id                           = Convert.ToInt32(tableProperties.Rows[0][TablePropertyDesc.Id]);
            tableProperty.Name                         = Convert.ToString(tableProperties.Rows[0][TablePropertyDesc.Name]);
            tableProperty.MinConsumption               = Convert.ToInt32(tableProperties.Rows[0][TablePropertyDesc.MinConsumption]);
            tableProperty.State                        = Convert.ToInt32(tableProperties.Rows[0][TablePropertyDesc.State]);
            tableProperty.Remark                       = Convert.ToString(tableProperties.Rows[0][TablePropertyDesc.Remark]);

            return tableProperty;
        }

        public static TableProperty GetTableProperty(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TablePropertyDesc.Name, ConditionExpress.EQUALS, name);

            TableProperty[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static TableProperty[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + TablePropertyDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                TableProperty[] tableProperties = new TableProperty[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    TableProperty tableProperty = new TableProperty();

                    tableProperty.Id                     = Convert.ToInt32(table.Rows[i][TablePropertyDesc.Id]);
                    tableProperty.Name                   = Convert.ToString(table.Rows[i][TablePropertyDesc.Name]);
                    tableProperty.MinConsumption         = Convert.ToInt32(table.Rows[i][TablePropertyDesc.MinConsumption]);
                    tableProperty.State                  = Convert.ToInt32(table.Rows[i][TablePropertyDesc.State]);
                    tableProperty.Remark                 = Convert.ToString(table.Rows[i][TablePropertyDesc.Remark]);

                    tableProperties.SetValue(tableProperty, i);
                }

                return tableProperties;
            }
            return null;
        }

        public static Int32 Add(TableProperty tableProperty)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(TablePropertyDesc.Name,                    tableProperty.Name);
            builder.InsertAdd(TablePropertyDesc.MinConsumption,          tableProperty.MinConsumption);
            builder.InsertAdd(TablePropertyDesc.State,                   tableProperty.State);
            builder.InsertAdd(TablePropertyDesc.Remark,                  tableProperty.Remark);
            builder.QueryTableName = TablePropertyDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TablePropertyDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = TablePropertyDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(TableProperty tableProperty)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(TablePropertyDesc.Name,                         tableProperty.Name);
            builder.UpdateAdd(TablePropertyDesc.MinConsumption,               tableProperty.MinConsumption);
            builder.UpdateAdd(TablePropertyDesc.State,                        tableProperty.State);
            builder.UpdateAdd(TablePropertyDesc.Remark,                       tableProperty.Remark);
            builder.ConditionAdd(TablePropertyDesc.Id, ConditionExpress.EQUALS, tableProperty.Id);
            builder.QueryTableName = TablePropertyDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
